package model

import (
	"Im/server/system/global"
	"Im/server/util"
)

type MessageModel struct {
	Base
}

type Message struct {
	Mid         uint64 `gorm:"primaryKey;column:mid;type:int(12)"`
	From_id     uint64 `gorm:"column:from_id;type:int(12);not null;"`
	To_id       uint64 `gorm:"column:to_id;type:int(12);not null"`
	Content     string `gorm:"column:content;type:varchar(255);not null"`
	Create_time uint64 `gorm:"column:create_time;type:int(12);not null"`
}

type MsgUser struct {
	Message
	Id            uint64 `gorm:"column:id;type:int(12)"`
	Username      string `gorm:"column:username;type:varchar(255);not null;"`
	Salt          string `gorm:"column:salt;type:varchar(64);not null"`
	Nickname      string `gorm:"column:nickname;type:varchar(255);not null"`
	Realname      string `gorm:"column:realname;type:varchar(255);not null"`
	Avatar        string `gorm:"column:avatar;type:varchar(255);not null"`
	Sex           uint8  `gorm:"column:sex;type:int(1);not null"`
	Age           uint8  `gorm:"column:age;type:int(3);not null"`
	Phone         string `gorm:"column:phone;type:varchar(12);not null"`
	Email         string `gorm:"column:email;type:varchar(255);not null"`
	Lastloginip   string `gorm:"column:lastloginip;type:varchar(20);not null"`
	Lastlogintime int64  `gorm:"column:lastlogintime;type:varchar(255);not null"`
	Status        uint8  `gorm:"column:status;type:int(1);not null"`
}

func (m *MessageModel) tabName() string {
	return global.JLZS_CONFIG.Mysql.Prefix + "message"
}

// 详情
func (m *MessageModel) Detail(id uint64, where map[string]string) (Message, error, bool) {

	var user Message
	err := global.JLZS_DB.Where(&Message{}).First(&user)
	if err.Error != nil {
		return user, err.Error, false
	}
	return user, err.Error, true
}

// 查询信息
func (m *MessageModel) ListData(id uint64, where map[string]string) ([]Message, error, bool) {

	var user []Message
	err := global.JLZS_DB.Where(&Message{}).Order("mid DESC").Find(&user)
	if err.Error != nil {
		return user, err.Error, false
	}
	return user, err.Error, true
}

// 查询新信息
func (m *MessageModel) NewMsg(id uint64, where map[string]string) ([]MsgUser, error) {

	dbMsgName := m.tabName()
	var msgGroup []Message
	err := global.JLZS_DB.Raw("SELECT from_id FROM "+dbMsgName+" WHERE to_id=? GROUP BY from_id", id).Scan(&msgGroup)
	if err.Error != nil {
		return nil, err.Error
	}
	msglen := len(msgGroup)
	var msgUserVal []MsgUser
	if msglen > 0 {
		msgArr := make([]uint64, 0, msglen)
		for i := 0; i < msglen; i++ {
			msgArr = append(msgArr, msgGroup[i].From_id)
		}
		userObj := new(UserModel)
		global.JLZS_DB.Raw("SELECT m.*,u.* FROM "+dbMsgName+" AS m LEFT JOIN "+userObj.tabName()+" AS u ON m.from_id=u.id WHERE m.to_id=? AND u.id IN ?", id, msgArr).Scan(&msgUserVal)
	}
	return msgUserVal, nil
}

// 新增消息
func (m *MessageModel) AddMsg(msg map[string]string) bool {
	messageC := &Message{
		From_id:     util.StrToUint64(msg["from_id"]),
		To_id:       util.StrToUint64(msg["to_id"]),
		Content:     msg["content"],
		Create_time: uint64(util.Time()),
	}
	result := global.JLZS_DB.Create(&messageC)
	if result.Error != nil {
		return false
	}
	return true
}
